
package com.emser.unc.server.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.emser.unc.serializable.UserAccountDTO;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class UserAccount {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key userName;
	@Persistent
	private Date lastLoginOn;
	@Persistent
	private Date stockUpdateLastDate = new Date();
	@Persistent(mappedBy= "userA")
	private List<Seller> sellers = new ArrayList<Seller>(); 
	@Persistent(mappedBy= "userA")
	private List<Customer> customers = new ArrayList<Customer>(); 
	@Persistent(mappedBy= "userA")
	private List<Product> products = new ArrayList<Product>(); 
	@Persistent(mappedBy= "userA")
	private List<SaleReport> saleReports = new ArrayList<SaleReport>(); 
	@Persistent(mappedBy= "userA")
	private List<NoSaleReport> saleNoReports = new ArrayList<NoSaleReport>(); 

	public UserAccount(String userName) {
		this.setUserName(userName);
	}

	public Key getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = KeyFactory.createKey(UserAccount.class.getSimpleName(), userName);
	}

	public static UserAccountDTO toDTO(UserAccount user) {
		if (user == null) {
			return null;
		}
		UserAccountDTO accountDTO = new UserAccountDTO(user.getUserName().getName());
		accountDTO.setLastUpdateDate(user.getStockUpdateLastDate());
		return accountDTO;
	}
	public void setLastLoginOn(Date lastLoginOn) {
		this.lastLoginOn = lastLoginOn;
	}

	public Date getLastLoginOn() {
		return lastLoginOn;
	}

	public void addSeller(Seller seller) {
		this.sellers.add(seller);
	}

	public List<Seller> getSellers() {
		return sellers;
	}

	public void addCustomer(Customer customer) {
		this.customers.add(customer);
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void addProduct(Product product) {
		this.products.add(product);
	}

	public List<Product> getProducts() {
		return products;
	}

	public void addSaleReport(SaleReport saleReport) {
		this.saleReports.add(saleReport);
	}

	public List<SaleReport> getSaleReports() {
		return saleReports;
	}

	public void addSaleNoReport(NoSaleReport saleNoReport) {
		this.saleNoReports.add(saleNoReport);
	}

	public List<NoSaleReport> getSaleNoReports() {
		return saleNoReports;
	}

	public void setStockUpdateLastDate(Date stockUpdateDate) {
		this.stockUpdateLastDate = stockUpdateDate;
	}

	public Date getStockUpdateLastDate() {
		return stockUpdateLastDate;
	}

}
